﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace _0912089_0912172
{
    public class KeyBoardMapping
    {
        public enum GameKeys { Up,Down,Right,Left,Power,Defend,Jump};
   
        Keys _up = Keys.W;
        Keys _down = Keys.S;
        Keys _right = Keys.A;
        Keys _left = Keys.D;
        Keys _power = Keys.E;
        Keys _defend = Keys.Q;
        Keys _jump = Keys.Space;



        bool _isUpPress;
        bool _isDownPress;
        bool _isRightPress;
        bool _isLeftPress;
        bool _isPowerPress;
        bool _isDefendPress;
        bool _isJumpPress;
        List<GameKeys> _newGameKeysPressed;
        List<GameKeys> _newGameKeysReleased;
        List<GameKeys> _listGameKeys;

        public List<GameKeys> NewGameKeysPressed
        {
            get { return _newGameKeysPressed; }
            //set { _newGameKeysPressed = value; }
        }
        public List<GameKeys> NewGameKeysReleased
        {
            get { return _newGameKeysReleased; }
           // set { _newGameKeysReleased = value; }
        }

        public KeyBoardMapping()
        {
            _newGameKeysPressed = new List<GameKeys>();
            _newGameKeysReleased = new List<GameKeys>();
            _listGameKeys = new List<GameKeys>();
            _listGameKeys.Add(GameKeys.Up);
            _listGameKeys.Add(GameKeys.Down);
            _listGameKeys.Add(GameKeys.Left);
            _listGameKeys.Add(GameKeys.Right);
            _listGameKeys.Add(GameKeys.Power);
            _listGameKeys.Add(GameKeys.Defend);
            _listGameKeys.Add(GameKeys.Jump);
            refresh();
        }
        public void refresh()
        {
            KeyboardState keyState = Keyboard.GetState();
            _isUpPress = keyState.IsKeyDown(_up);
            _isDownPress = keyState.IsKeyDown(_down);
            _isRightPress = keyState.IsKeyDown(_right);
            _isLeftPress= keyState.IsKeyDown(_left);
            _isPowerPress= keyState.IsKeyDown(_power);
            _isDefendPress = keyState.IsKeyDown(_defend);
            _isJumpPress = keyState.IsKeyDown(_jump);
        }
        public Keys Power
        {
            get { return _power; }
            set { _power = value; }
        }
        public Keys Left
        {
            get { return _left; }
            set { _left = value; }
        }
        public Keys Right
        {
            get { return _right; }
            set { _right = value; }
        }
        public Keys Down
        {
            get { return _down; }
            set { _down = value; }
        }        
        public Keys Up
        {
            get { return _up; }
            set { _up = value; }
        }

        public Keys Defend
        {
            get { return _defend; }
            set { _defend = value; }
        }
        public Keys Jump
        {
            get { return _jump; }
            set { _jump = value; }
        }
        public Keys getKeyFromGameKey(GameKeys k)
        {
            switch (k)
            {
                case GameKeys.Up : return _up;
                case GameKeys.Down: return _down;
                case GameKeys.Right: return _right;
                case GameKeys.Left: return _left;
                case GameKeys.Power: return _power;
                case GameKeys.Defend: return _defend;
                case GameKeys.Jump: return _jump;
            }
            return Keys.None;
        }
        public bool isGameKeyPress(GameKeys k)
        {
            switch (k)
            {
                case GameKeys.Up: return _isUpPress;
                case GameKeys.Down: return _isDownPress;
                case GameKeys.Right: return _isRightPress;
                case GameKeys.Left: return _isLeftPress;
                case GameKeys.Power: return _isPowerPress;
                case GameKeys.Defend: return _isDefendPress;
                case GameKeys.Jump: return _isJumpPress;
            }
            return false;
        }
        public bool isJustPressed(GameKeys k)
        {
            foreach (GameKeys key in _newGameKeysPressed)
            {
                if (key == k)
                    return true;
            }
            return false;
        }
        public bool isJustReleased(GameKeys k)
        {
            foreach (GameKeys key in _newGameKeysReleased)
            {
                if (key == k)
                    return true;
            }
            return false;
        }
        public void upDateKeys()
        {
            

            KeyboardState keyState = Keyboard.GetState();

            _newGameKeysPressed.Clear();
            _newGameKeysReleased.Clear();

            for (int i = 0; i < _listGameKeys.Count; i++)
            {
                if (isGameKeyPress(_listGameKeys[i]) &&
                    keyState.IsKeyUp(getKeyFromGameKey(_listGameKeys[i])))
                {
                    _newGameKeysReleased.Add(_listGameKeys[i]);
                }
                else if (!isGameKeyPress(_listGameKeys[i]) &&
                    keyState.IsKeyDown(getKeyFromGameKey(_listGameKeys[i]))
                    )
                {
                    _newGameKeysPressed.Add(_listGameKeys[i]);
                }
            }
            refresh();


        }
        
    }
       
}
